package com.loongtech.bi.action.system;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.loongtech.bi.entity.system.EntityBiServer;
import com.loongtech.bi.entity.system.EntitySysRole;
import com.loongtech.bi.manager.system.BiServerManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.manager.system.SysRoleManager;
import com.loongtech.bi.manager.system.SysRoleServerManager;
import com.loongtech.bi.support.Session;

/**
 * 单个角色游戏服列表设置
 * @author xckuang
 * @date 2015-05-22
 */
@Controller("roleServManager")
@Scope("view")
public class RoleServManager implements Serializable
{
	private static final long			serialVersionUID	= 1L;
	@Autowired
	private Session						session;
	@Autowired
	private SysLogManager				sysLogManager;
	@Autowired
	private SysRoleManager				sysRoleManager;
	@Autowired
	private BiServerManager				serverManager;
	@Autowired
	private SysRoleServerManager		sysRoleServerManager;
	private Map<Integer, Boolean>		selected			= new HashMap<>();
	private Map<Integer, Boolean>		unselected			= new HashMap<>();
	private int							roleId;
	private Map<Integer, EntitySysRole>	roleMap				= new HashMap<Integer, EntitySysRole>();
	private List<EntityBiServer>		leftList			= new ArrayList<>();		// 可选择列表
	private List<EntityBiServer>		rightList			= new ArrayList<>();		// 已经选择列表

	@Autowired
	public void init()
	{
		roleId = 0;
		roleMap.clear();
		for (EntitySysRole role : sysRoleManager.findAll())
		{
			roleMap.put(role.getId(), role);
		}
		change();
	}

	public void select()
	{
		for (EntityBiServer server : leftList)
		{
			Boolean val = selected.get(server.getId());
			if (null != val && val.booleanValue())
			{
				rightList.add(server);
			}
		}
		selected.clear();
		leftList.removeAll(rightList);
	}

	public void unSelect()
	{
		for (EntityBiServer server : rightList)
		{
			Boolean val = unselected.get(server.getId());
			if (null != val && val.booleanValue())
			{
				leftList.add(server);
			}
		}
		unselected.clear();
		rightList.removeAll(leftList);
	}

	/**
	 * 保存角色服务器信息
	 */
	public void save()
	{
		FacesContext context = FacesContext.getCurrentInstance();
		EntitySysRole role = sysRoleManager.get(roleId);
		if (null == role)
		{
			context.addMessage(null, new FacesMessage("修改角色服务器列表失败", "修改角色服务器列表失败，角色不存在 roleId=" + roleId));
			return;
		}
		// 当前集合
		Set<String> oldSet = sysRoleServerManager.getRoleServerSet(roleId);

		// newSet of id
		Set<String> newSet = getSet(rightList);
		Set<String> submit = new HashSet<String>(oldSet);
		submit.retainAll(newSet);
		oldSet.removeAll(submit); // to remove
		newSet.removeAll(submit); // to insert
		// 删除oldSet
		for (String serverId : oldSet)
		{
			sysRoleServerManager.removeByRoleIdAndServerId(roleId, serverId);
		}
		// 插入newSet
		for (String serverId : newSet)
		{
			sysRoleServerManager.insertByRoleIdAndServerId(roleId, serverId);
		}
		sysLogManager.addLog(session.getUserName(), "修改角色服务器列表成功" + role.getName());
		context.addMessage(null, new FacesMessage("修改角色服务器列表成功", "修改角色服务器列表成功，角色名称为=" + role.getName()));
		change();
	}

	private Set<String> getSet(List<EntityBiServer> list)
	{
		Set<String> curSet = new HashSet<String>();
		if (null == list || list.isEmpty())
		{
			return curSet;
		}
		for (EntityBiServer info : list)
		{
			if (null != serverManager.get(info.getId()))
			{
				curSet.add(info.getId());
			}
		}
		return curSet;
	}

	public void change()
	{
		selected.clear();
		unselected.clear();
		EntitySysRole role = sysRoleManager.get(roleId);
		if (null == role)
		{
			leftList.clear();
			rightList.clear();
			leftList.addAll(serverManager.getByUserId(session.getUserId()));
			return;
		}
		// 先初始化rightList
		rightList.clear();
		rightList = sysRoleServerManager.getRoleServerList(roleId);
		leftList.clear();
		leftList.addAll(serverManager.getByUserId(session.getUserId()));
		leftList.removeAll(rightList);
	}

	public int getRoleId()
	{
		return roleId;
	}

	public void setRoleId(int roleId)
	{
		this.roleId = roleId;
	}

	public Map<Integer, EntitySysRole> getRoleMap()
	{
		return roleMap;
	}

	public void setRoleMap(Map<Integer, EntitySysRole> roleMap)
	{
		this.roleMap = roleMap;
	}	

	public Map<Integer, Boolean> getSelected()
	{
		return selected;
	}

	public void setSelected(Map<Integer, Boolean> selected)
	{
		this.selected = selected;
	}

	public Map<Integer, Boolean> getUnselected()
	{
		return unselected;
	}

	public void setUnselected(Map<Integer, Boolean> unselected)
	{
		this.unselected = unselected;
	}

	public List<EntityBiServer> getLeftList()
	{
		return leftList;
	}

	public void setLeftList(List<EntityBiServer> leftList)
	{
		this.leftList = leftList;
	}

	public List<EntityBiServer> getRightList()
	{
		return rightList;
	}

	public void setRightList(List<EntityBiServer> rightList)
	{
		this.rightList = rightList;
	}
}
